package com.jxb.delay;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * 延时消息
 *
 * @author jiaobo
 * @date Created in 2024/9/10 21:46
 **/
public class ScheduledMessageProducer {

    public static void main(String[] args)
            throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup1");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();

        int totalMessagesToSend = 100;
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message message = new Message("TestTopic1", ("Hello scheduled message " + i).getBytes());
            //messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
            message.setDelayTimeLevel(3);
            producer.send(message);
        }

        producer.shutdown();
    }

}
